﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;
using UniversityStudentManagemnetSystem.DAO.DAL.Gateway;

namespace UniversityStudentManagemnetSystem.BLL
{
    public class TeacherManager:Manager
    {
        private TeacherGateway teacherGateway;
        public TeacherManager()
        {
              teacherGateway = new TeacherGateway();
        }

        public DataTable GetAllTeachersFor(int selectedDepartment)
        {
            return teacherGateway.GetAllTeacherFor(selectedDepartment);
        }

        public string Save(Teacher teacher)
        {
            if (!IsValidString(teacher.TeacherName) && !IsValidString(teacher.Address) && !IsEmail(teacher.Email) && !IsContactNo(teacher.ContactNumber))
            {
               throw  new Exception( "Please, fill up the all fields correctly.");
            }
            else if (!IsValidString(teacher.TeacherName))
            {
                throw  new Exception("Please, fill up the Name.");
            }
            else if (!IsValidString(teacher.Address))
            {
                throw new Exception("Please, fill up the Address.");
            }
            else if (!IsEmail(teacher.Email))
            {
                throw new Exception("Please, fill up the Email Address.");
            }
            else if (!IsContactNo(teacher.ContactNumber))
            {
                throw new Exception("Please, fill up the Contact Number");
            }

            bool teacherEmailExist = teacherGateway.HasThisEmail(teacher.Email);
            if (teacherEmailExist)
            {
                throw new Exception("This Teracher already exists.\n Please try again.");
            }
            else
            {
                if (teacherGateway.TeacherSave(teacher))
                {
                    return "This Teacher is added to the list";
                }
                else
                {
                    throw new Exception("Unknown Error");
                }
            }
        }

        public DataTable GetAllTeachers()
        {
          return  teacherGateway.GetAllTeachers();
        }

        public Teacher GetTeacherForNameOrEmail(string nameOrEmail)
        {
            Teacher aTeacher = teacherGateway.GetTeacherPersonalInformation(nameOrEmail);
            return aTeacher;
        }
    }
}